using System.Collections;
using System.Collections.Generic;
using Core.Mgr._Timer;
using Newtonsoft.Json;
using QQSDK;
using UnityEngine;

namespace Core.Mgr
{
    /// <summary>
    /// 打点管理器
    /// </summary>
    public static class DaDianMgr
    {
        public static void OnInit()
        {
        }

        /// 设置用户属性
        public static void SetUserProperty(string key, object value, bool isOnce = true)
        {
            Dictionary<string, object> attributionDict = new Dictionary<string, object>();
            attributionDict.Add(key, value);
            string jsonStr = JsonConvert.SerializeObject(attributionDict);
            AnalyticsManager.SetUserProperty(jsonStr, isOnce);
            Debug.Log($"<Color=#1FFF00>SetUserProperty</Color>***|{jsonStr}");
        }

        public static void Show(string action, string key1 = null, object value1 = null,
            string key2 = null, object value2 = null, string key3 = null, object value3 = null,
            string key4 = null, object value4 = null, string key5 = null, object value5 = null, float delay = 0)
        {
            var array = CreateArrayList(key1, value1, key2, value2, key3, value3, key4, value4, key5, value5);
            if (delay is 0)
                AnalyticsManager.LogShowEvent(action, array);
            else
                TimerMgr.StartTimer(delegate { AnalyticsManager.LogShowEvent(action, array); }, delay);
        }

        public static void Task(string action, string key1 = null, object value1 = null,
            string key2 = null, object value2 = null, string key3 = null, object value3 = null,
            string key4 = null, object value4 = null, string key5 = null, object value5 = null, float delay = 0)
        {
            var array = CreateArrayList(key1, value1, key2, value2, key3, value3, key4, value4, key5, value5);
            if (delay is 0)
                AnalyticsManager.LogTaskEvent(action, array);
            else
                TimerMgr.StartTimer(delegate { AnalyticsManager.LogTaskEvent(action, array); }, delay);
        }

        public static void Click(string action, string key1 = null, object value1 = null,
            string key2 = null, object value2 = null, string key3 = null, object value3 = null,
            string key4 = null, object value4 = null, string key5 = null, object value5 = null, float delay = 0)
        {
            var array = CreateArrayList(key1, value1, key2, value2, key3, value3, key4, value4, key5, value5);
            if (delay is 0)
                AnalyticsManager.LogClickEvent(action, array);
            else
                TimerMgr.StartTimer(delegate { AnalyticsManager.LogClickEvent(action, array); }, delay);
        }

        private static ArrayList CreateArrayList(string key1 = null, object value1 = null,
            string key2 = null, object value2 = null, string key3 = null, object value3 = null,
            string key4 = null, object value4 = null, string key5 = null, object value5 = null)
        {
            ArrayList arrayList = null;
            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (!string.IsNullOrEmpty(key1) && value1 != null)
                dic.Add(key1, value1);
            if (!string.IsNullOrEmpty(key2) && value2 != null)
                dic.Add(key2, value2);
            if (!string.IsNullOrEmpty(key3) && value3 != null)
                dic.Add(key3, value3);
            if (!string.IsNullOrEmpty(key4) && value4 != null)
                dic.Add(key4, value4);
            if (!string.IsNullOrEmpty(key5) && value5 != null)
                dic.Add(key5, value5);
            if (dic.Count != 0)
            {
                arrayList = new ArrayList();
                arrayList.Add(dic);
            }

            return arrayList;
        }
    }
}